<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>06-div开始和停止移动</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 150px;
            height: 150px;
            background-color: wheat;
        }
    </style>
</head>
<body>
    <button>开启</button>
    <br/>
    <button>停止</button>
    <div>一路向北</div>

    <script>
        // 点击按钮 div 往右移动 某个位置后停止

        let btns = document.querySelectorAll('button');
        let div = document.querySelector('div');

        let timer = null;        // 声明一下 如果后面删除定时器不会出现bug
        btns[0].addEventListener('click',function(){
            console.log('开始移动');

            timer = setInterval(function(){
                let left = div.offsetLeft;
                div.style.left = left + 20 + 'px';
            },500);
        });
        btns[1].addEventListener('click',function(){
            console.log('停止移动');
            clearInterval(timer);
        });
    </script>
</body>
</html>